import React, {Component} from 'react'
import {Provider} from 'mobx-react'

import counterStore from './store/counter'
import globalVal from './store/store'

import './app.scss'

const store = {
    counterStore
}

class App extends Component {
    async componentDidMount() {
        await this.checkUserData()
    }

    componentDidShow() {
    }

    componentDidHide() {
    }

    componentDidCatchError() {
    }

    checkUserData = () => {
        let url = {
            index: 'pages/index/index',
            user: 'pages/user/index'
        }
        wx.onAppRoute(route => { // 获取将要跳转的路由
            if (route.path !== url.index && route.path !== url.user) {
                if (!globalVal.globalData.userInfo.username) {
                    wx.showModal({
                        title: '提示',
                        content: '测试',
                        success(res) {
                            if (res.confirm) {
                                wx.switchTab({
                                    url: '/'
                                })
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })
                    return
                }

                if (globalVal.globalData.userInfo.username && !globalVal.globalData.userInfo.idCard) {
                    wx.showModal({
                        title: '实名认证',
                        content: '实名认证',
                        success(res) {
                            if (res.confirm) {
                                wx.switchTab({
                                    url: '/'
                                })
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })
                    return
                }
            }
        })
    }


    // this.props.children 就是要渲染的页面
    render() {
        return (
            <Provider store={store}>
                {this.props.children}
            </Provider>
        )
    }
}

export default App
